package com.gh.ok.ui

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import com.gh.ok.R
import com.gh.ok.const.Const.getPermissions
import com.gh.ok.const.EventConst
import com.gh.ok.databinding.ActivityMainBinding
import com.gh.ok.entity.UserInfo
import com.gh.ok.utils.PermissionUtils
import com.gh.ok.utils.RequestCallBack
import com.gyf.immersionbar.ktx.immersionBar
import com.ok.common.base.BaseActivity
import com.ok.common.base.BaseEvent
import com.ok.common.ext.launch
import com.ok.common.ext.toast

class MainActivity : BaseActivity<ActivityMainBinding>() {

    /**
     * 注册EventBus事件
     */
    override var isRegisterEvent = true

    /**
     * 接受EventBus事件
     */
    override fun handleEvent(event: BaseEvent) {
        when (event.code) {
            EventConst.LOGIN_SUCCESS -> {
                event.data?.apply {
                    toast("收到携带数据事件：${this as UserInfo}")
                } ?: run {
                    toast("收到普通事件")
                }
            }
        }
    }

    override fun initImmersionBar() {
        immersionBar {
            titleBar(mBinding.viewTitle.titleBar)
            navigationBarColor(R.color.bgHomeTab)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.viewTitle.ibtnBack.visibility = View.INVISIBLE
        mBinding.viewTitle.tvTitle.text = getString(R.string.app_name)
        PermissionUtils.apply(this, object : RequestCallBack {
            override fun granted() {
            }

            override fun denied(deniedList: List<String?>?) {
            }

        }, getPermissions())
    }

    override fun initListener() {
        mBinding.btnHttpSample.setOnClickListener(this)
        mBinding.btnCoilSample.setOnClickListener(this)
        mBinding.btnEventbusSample.setOnClickListener(this)
        mBinding.btnFlowSample.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.btn_http_sample -> {
                launch(
                    bundleOf("title" to mBinding.btnHttpSample.text.toString()),
                    HttpSampleActivity::class.java
                )
            }

            R.id.btn_coil_sample -> {
                launch(
                    bundleOf("title" to mBinding.btnCoilSample.text.toString()),
                    CoilSampleActivity::class.java
                )
            }

            R.id.btn_eventbus_sample -> {
                launch(
                    bundleOf("title" to mBinding.btnEventbusSample.text.toString()),
                    EventBusSampleActivity::class.java
                )
            }

            R.id.btn_flow_sample -> {
                launch(
                    bundleOf("title" to mBinding.btnFlowSample.text.toString()),
                    FlowSampleActivity::class.java
                )
            }
        }
    }

}